package com.park.client.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.util.Assert;


public class StringToByte {
    /**
     * @description[将ip地址转为一个byte[]]
     * @date 2014年11月17日 上午9:53:00
     * @version v1.0
     */
    public static byte[] StringToByte(String ip) {
        String[] ips = ip.split("\\.");
        byte[] a = new byte[4];
        for (int i = 0; i < ips.length; i++) {
            a[i] = (byte) Integer.parseInt(ips[i]);
        }
        return a;
    }


    /**
     * @description[int转为byte[]]
     * @date 2014年11月17日 上午9:52:38
     * @version v1.0
     */
    public static byte[] intToByte(int i) {
        byte[] a = intToBytes2(i);
        return a;
    }


    private static byte[] toByte(Object... obj) {
        Object n = obj[0];
        byte[] b = null;
        long val = 0;
        if (n instanceof Short) {
            b = new byte[2];
            val = (Short) n;
        }
        if (n instanceof Integer) {
            b = new byte[4];
            val = (Integer) n;
        }
        if (n instanceof Long) {
            b = new byte[8];
            val = (Long) n;
        }
        for (int i = 0; i < b.length; i++) {
            b[i] = (byte) (val >> ((b.length - 1 - i) * 8));
        }
        return b;
    }


    /** 转1个字节byte[] */
    public static byte[] byteToBytes2(byte n) {
        byte[] b = { n };
        return b;
    }


    /** 转4个字节byte[] */
    public static byte[] intToBytes2(int n) {
        return toByte(n);
    }


    /** 转2个字节byte[] */
    public static byte[] shortToBytes2(short n) {
        return toByte(n);
    }


    /** 转8个字节byte[] */
    public static byte[] longToBytes2(long n) {
        return toByte(n);
    }


    /** 转指定长度byte[] */
    public static byte[] longToBytes2(long n, int len) {
        byte[] b = toByte(n);
        Assert.isTrue(len <= 8);
        byte[] result = new byte[len];
        for (int i = 0; i < len; i++) {
            result[i] = b[i];
        }
        return result;
    }


    /** code转byte[] */
    public static byte[] codeToByte(String str) {
        List<String> list = new ArrayList<String>();
        while (str.length() > 2) {
            String temp = str.substring(0, 2);
            list.add(temp);
            str = str.substring(2, str.length());
        }
        if (str.length() > 0) {
            list.add(str);
        }

        String[] arrs = new String[list.size()];
        for (int i = 0; i < list.size(); i++) {
            arrs[i] = list.get(i);
        }

        byte[] a = new byte[3];
        for (int i = 0; i < arrs.length; i++) {
            a[i] = (byte) Integer.parseInt(arrs[i]);
        }
        return a;
    }
    
    
    /**
     * @description[计算时间差方法]
     * @date   2015年3月27日 上午9:55:41 
     * @return
     * @throws ParseException
     */
    public static long timeDifference(Object heartbeAt,Date systemTime) throws ParseException{
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Date date = sdf.parse(sdf.format(heartbeAt));//将取出的心跳时间转为date类型
        long heartbeAtMs = date.getTime();//取出最新心跳时间的毫秒数
        long systemMS = systemTime.getTime();//当前系统时间毫秒数
        long difference = (systemMS - heartbeAtMs)/1000;//系统时间与最新心跳时间差
        return difference;
    }
    
}
